<?php
/**
 * View class
 * @author Joe Minichino <joe@dsforge.net>
 * 
 */

class View {
    private $file;
    private $data;
    function  __construct($filename) {
        Debug::log("Creating view $filename");
        $this->file = $filename;
    }

    public static function load($file){

        return new View($file);
    }

    function __set($name, $value){
        
        Debug::log("Setting variable $name view " . $this->file);
        
        $this->data[$name] = $value;
        return $this;
    }

    function __get($name){
        return $this->data[$name];
    }

    public function render($buffer=false){
        Debug::log("Rendering view " . $this->file);
        // extract variables to current scope
        @extract($this->data, EXTR_SKIP);
        ob_start();
        try {
            include "views/" . $this->file .".php";
        } catch(Exception $e){
            Debug::log("Error including file " . $this->file . ".php");
            ob_end_clean();
        }

        if($buffer==true){
            return ob_get_clean();
        } else {
            echo ob_get_clean();
        }

    }

}
